package com.socialrss.viewlisteners;

import java.util.ArrayList;
import java.util.List;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Spinner;

import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.socialrss.R;
import com.socialrss.adapters.CustomListAdapter;
import com.socialrss.adapters.SpAdapter;
import com.socialrss.utils.NotificationBroadcastReceiver;

public class MainActivity extends BaseActivity {
	private ArrayList<NotificationItem> notList = new ArrayList<NotificationItem>();
	private int numberElement = 0;
	CustomListAdapter customAdapter;
	private NotificationBroadcastReceiver alarm;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		alarm = new NotificationBroadcastReceiver();
		alarm.SetAlarm(this.getApplicationContext());
		
		// fill the list
		String word = "Fazli Can added new comment to his social network";
		for (int i = 0; i < 8; i++) {
			word = i + "th " + word;
			notList.add(new NotificationItem(numberElement%4,word,numberElement));
			numberElement++;
			word = "Fazli Can added new comment to his social network";
		}		
		
		// spinner part
		Spinner spinner = (Spinner) findViewById(R.id.spin);
		// Create an ArrayAdapter using the string array and a default spinner layout
		
		// Specify the layout to use when the list of choices appears
		ArrayList<String> groupList = new ArrayList<String>();
		final ArrayList<Integer> groupListId = new ArrayList<Integer>();
		// fill the earliest
		groupList.add("All Groups");
		groupListId.add(-1);
		groupList.add("Friends");
		groupList.add("Sports");
		groupList.add("Magazine");
		groupList.add("Politics");
		groupListId.add(1);
		groupListId.add(2);
		groupListId.add(3);
		groupListId.add(4);
		final SpAdapter adapter = new SpAdapter(groupList,this.getApplicationContext(), groupListId); // add group id here
		// Apply the adapter to the spinner
		
//		spinner.setPrompt("Choose a Group");
        spinner.setAdapter(adapter);
        /*
        spinner.setOnItemClickListener(new OnItemClickListener() {
        	
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				int number = adapter.getItemGroupId(arg2);
				String not = String.valueOf(number);
				Intent intent = new Intent(MainActivity.this, NotificationOpen.class);
				intent.putExtra("notify", not);
				MainActivity.this.startActivity(intent);
				
			}
        	
        	
        });
		
		*/
		// definition of adapter and setting
		customAdapter = new CustomListAdapter(this, notList);
		ListView lv = (ListView) findViewById(R.id.notifocation_list);
		lv.setAdapter(customAdapter);
		lv.setTextFilterEnabled(true);
		lv.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				String not = notList.get(arg2).text;
				Intent intent = new Intent(MainActivity.this, NotificationOpen.class);
				intent.putExtra("notify", not);
				MainActivity.this.startActivity(intent);
			}

		});
		// button part
//		buttonDialog = (Button) findViewById(R.id.button1);
		// set button listener
//		buttonDialog.setOnClickListener(listenerDialogAlert);
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if(item.getItemId() == R.id.github1) {
			alarm.CancelAlarm(getApplicationContext());
		}
		if(item.getItemId()==R.id.refresh){
			// create the full list
		}
		else if(item.getItemId()==R.id.adjust){
			Builder builder = new AlertDialog.Builder(MainActivity.this);
			AlertDialog dialog = builder.create();
			dialog.setTitle("Adjust your list");
			dialog.setMessage("You can adjust your list here");
			dialog.setButton2("Social Network", social);
			dialog.setButton3("Groups", groups);
			dialog.setButton("Time", time);
			dialog.show();		
		}
		else if(item.getItemId()==R.id.clear){
			Builder b = new AlertDialog.Builder(MainActivity.this);
			AlertDialog dialog = b.create();
			dialog.setTitle("Clear");
			dialog.setMessage("You can clear all or select them");
			dialog.setButton2("Clear", clear);
			dialog.setButton("Clear All", clearAll);
			dialog.show();	
		}
		
		return super.onOptionsItemSelected(item);
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getSupportMenuInflater().inflate(R.menu.options_component, menu);
		return true;
	}


	DialogInterface.OnClickListener social = new DialogInterface.OnClickListener() {

		public void onClick(DialogInterface dialog, int which) {		
			regulateSocial(notList);
			customAdapter = new CustomListAdapter(MainActivity.this, notList);
			ListView lv = (ListView) findViewById(R.id.notifocation_list);
			lv.setAdapter(customAdapter);
			lv.setTextFilterEnabled(true);
		}
		public void regulateSocial(List<NotificationItem> list){
			NotificationItem[] tempList = new NotificationItem[list.size()];
			for(int i=0;i<list.size();i++){
				tempList[i]=list.get(i);
			}
			list.clear();
			
			for(int i=0;i<5;i++){
				for(int j=0; j<tempList.length; j++){
					if(tempList[j].photoType==i){
						list.add(tempList[j]);
					}
				}
			}
			
		}
	};

	DialogInterface.OnClickListener groups = new DialogInterface.OnClickListener() {

		public void onClick(DialogInterface dialog, int which) {
			// setContentView(R.layout.popup);
		}
	};
	DialogInterface.OnClickListener time = new DialogInterface.OnClickListener() {

		public void onClick(DialogInterface dialog, int which) {
			regulateTime(notList);
			customAdapter = new CustomListAdapter(MainActivity.this, notList);
			ListView lv = (ListView) findViewById(R.id.notifocation_list);
			lv.setAdapter(customAdapter);
			lv.setTextFilterEnabled(true);
		}

		private void regulateTime(ArrayList<NotificationItem> list) {
			NotificationItem[] tempList = new NotificationItem[list.size()];
			for(int i=0;i<list.size();i++){
				tempList[list.get(i).id]=list.get(i);
			}
			list.clear();
			for (int j = 0; j < tempList.length; j++) {
				list.add(tempList[j]);
			}
		}
	};
	
	// clear and clear all
	DialogInterface.OnClickListener clear = new DialogInterface.OnClickListener() {

		public void onClick(DialogInterface dialog, int which) {
			// setContentView(R.layout.popup);
		}
	};
	
	DialogInterface.OnClickListener clearAll = new DialogInterface.OnClickListener() {

		public void onClick(DialogInterface dialog, int which) {
			// setContentView(R.layout.popup);
		}
	};
	
	
	
}